﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Matrix.Common.Utility;
using Matrix.MaKaYunShang.DAL;
using Matrix.MaKaYunShang.Model;
using Matrix.MaKaYunShang.Model.Dto;

namespace Matrix.MaKaYunShang.BLL
{
    public class ShopsBLL : BaseBLL<ShopsBLL>
    {
        #region 默认方法

        /// <summary>
        ///     增加一条数据
        /// </summary>
        public int Add(Shops model)
        {
            return ShopsDAL.Instance.Add(model);
        }

        /// <summary>
        ///     更新一条数据
        /// </summary>
        public bool Update(Shops model)
        {
            return ShopsDAL.Instance.Update(model);
        }

        /// <summary>
        ///     删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {
            return ShopsDAL.Instance.Delete(Id);
        }

        /// <summary>
        ///     批量删除一批数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            return ShopsDAL.Instance.DeleteList(Idlist);
        }

        /// <summary>
        ///     得到一个对象实体
        /// </summary>
        public Shops GetModel(int Id)
        {
            DataSet ds = ShopsDAL.Instance.GetModel(Id);
            return ds == null ? null : ServicesHelper.DataSetToIList<Shops>(ds).ToList()[0];
        }

        /// <summary>
        ///     获得数据列表
        /// </summary>
        public List<Shops> GetModelList(string strWhere = null)
        {
            DataSet ds = ShopsDAL.Instance.GetList(strWhere);
            return ServicesHelper.DataSetToIList<Shops>(ds).ToList();
        }

        #endregion

        #region 扩展方法

        public Shops GetModelShopId(int id, string longitude, string latitude)
        {
            DataSet ds = ShopsDAL.Instance.GetModelShopId(id, longitude, latitude);
            return ds == null ? null : ServicesHelper.DataSetToIList<Shops>(ds).ToList()[0];
        }

        public DataTable GetModelListdt(string strWhere = null)
        {
            DataSet ds = ShopsDAL.Instance.GetLists(strWhere);
            return ds.Tables[0];
        }

        /// <summary>
        /// 根据店铺ID获取店铺管理级账号ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int GetShopSuperAccountId(int id)
        {
            var accDs = McAccountsDAL.Instance.GetSuperAccountsByShopId(id);
            if (accDs == null) return 0;

            var lsAccounts = ServicesHelper.DataSetToIList<McAccounts>(accDs).ToList();
            lsAccounts = lsAccounts.OrderBy(x => x.Created).ToList();

            return lsAccounts.Count > 0 ? lsAccounts[0].Id : 0;
        }

        /// <summary>
        /// 获得分页数据列表(与商家后台共用)
        /// </summary>
        public TableDataViewDto<ViewShopsDto> GetPagingList(int draw, int pageIndex, int pageSize, string searchKeyWord, string colOrder, List<MagModules> allowedModules = null, int parentShopId = 0)
        {
            try
            {
                var totalCount = 0;
                var list = ShopsDAL.Instance.GetPagingList(pageIndex, pageSize, searchKeyWord, colOrder, parentShopId, ref totalCount);
                var theReturnList = new List<ViewShopsDto>();
                if (list != null && list.Count > 0)
                {
                    var mcAccountList = McAccountsBLL.Instance.GetModelList("ShopId in (" + string.Join(",", list.Select(l => l.Id)) + ") and AccountType=1");
                    foreach (var shop in list)
                    {
                        var temp = new ViewShopsDto();
                        var mcAccount = mcAccountList.Find(l => l.ShopId == shop.Id);
                        if (mcAccount != null)
                        {
                            temp.ShopMagAccount = mcAccount.UserName;
                        }
                        temp.Address = shop.Address;
                        temp.Description = shop.Description;
                        temp.Id = shop.Id;
                        temp.LegalPerson = shop.LegalPerson;
                        temp.LegalPersonMobile = shop.LegalPersonMobile;
                        temp.Name = shop.Name;
                        temp.IsActive = shop.IsActive ? "是" : "否";
                        if (allowedModules != null)
                        {
                            if (allowedModules.FindAll(t => t
                            .Action == "Charge" && t
                                .Controller == "Shops").Count > 0)
                            {
                                temp.Action = "<a class='btn btn-info' href='javascript:;' onclick=charge(" + shop.Id +
                                ");>充值</a> ";
                            }
                            if (allowedModules.FindAll(t => t
                                .Action == "Update" && t
                                    .Controller == "Shops").Count > 0)
                            {
                                temp.Action += "<a class='btn btn-warning' href='javascript:;' onclick=updateData(" + shop.Id +
                                ");>修改</a> ";
                            }
                            if (allowedModules.FindAll(t => t
                            .Action.Equals("Delete") && t
                                .Controller.Equals("Shops")).Count > 0)
                            {
                                temp.Action += "<a class='btn btn-danger' href='javascript:;' onclick=deleteData(" + shop.Id + ");>删除</a> ";
                            }
                        }
                        if (parentShopId > 0)//商家后台总部管理旗下商家
                        {
                            temp.Action = "<a class='btn btn-warning' href='javascript:;' onclick=updateData(" + shop.Id + ");>修改</a> ";
                        }
                        theReturnList.Add(temp);
                    }
                }
                var returnDataList = new TableDataViewDto<ViewShopsDto>
                {
                    data = theReturnList,
                    draw = draw,
                    recordsFiltered = totalCount,
                    recordsTotal = totalCount
                };
                return returnDataList;
            }
            catch (Exception ex)
            {
                _fileLog.Error(" GetPagingList | Exception: " + ex);
                return null;
            }
        }

        /// <summary>
        ///     获取满足指定半径范围内的店铺列表
        /// </summary>
        /// <param name="longitude"></param>
        /// <param name="latitude"></param>
        /// <param name="radius"></param>
        /// <returns></returns>
        public List<Shops> GetMatchedShopList(string longitude, string latitude, float radius, string searchKeyword = "")
        {
            return ShopsDAL.Instance.GetMatchedShopList(longitude, latitude, radius, searchKeyword);
        }

        /// <summary>
        ///     获取用户访问过/购买过的店铺列表
        /// </summary>
        /// <param name="consumerId"></param>
        /// <returns></returns>
        public List<Shops> GetShopListOfConsumerVisited(int consumerId)
        {
            return ShopsDAL.Instance.GetShopListOfConsumerVisited(consumerId);
        }

        /// <summary>
        /// 获取店铺已完成订单收入总金额
        /// </summary>
        /// <param name="shopId"></param>
        /// <returns></returns>
        public double GetTotalIncomeOfShopId(int shopId)
        {
            return ShopsDAL.Instance.GetTotalIncomeOfShopId(shopId);
        }
        /// <summary>
        /// 获取当前店铺的提成佣金
        /// </summary>
        /// <param name="shopId"></param>
        /// <returns></returns>
        public double GetTotalShopCommission(int shopId)
        {
            return ShopsDAL.Instance.GetTotalShopCommission(shopId);
        }

        /// <summary>
        /// 获取店铺优惠券使用订单金额为0已完成订单收入总金额
        /// </summary>
        /// <param name="shopId"></param>
        /// <returns></returns>
        public double GetCashPayCouponOrderZeroPayTotalIncomeOfShopId(int shopId)
        {
            return ShopsDAL.Instance.GetCashPayCouponOrderZeroPayTotalIncomeOfShopId(shopId);
        }

        /// <summary>
        /// 获取店铺优惠券使用订单金额不为为0已完成订单中使用优惠券的商品收入总金额
        /// </summary>
        /// <param name="shopId"></param>
        /// <returns></returns>
        public double GetUnOnlinePayCouponOrderUnZeroPayTotalIncomeOfShopId(int shopId)
        {
            return ShopsDAL.Instance.GetUnOnlinePayCouponOrderUnZeroPayTotalIncomeOfShopId(shopId);
        }

        /// <summary>
        ///     获得店铺简明列表
        /// </summary>
        public List<ShopBriefDto> GetShopBriefList(string strWhere = null)
        {
            DataSet ds = ShopsDAL.Instance.GetList(strWhere);
            return ServicesHelper.DataSetToIList<ShopBriefDto>(ds).ToList();
        }

        /// <summary>
        /// 根据条件获取店铺总数
        /// </summary>
        /// <remarks>2016-08-22 万乐园 创建</remarks>
        public int GetTotalCount(string strWhere = null)
        {
            return ShopsDAL.Instance.GetTotalCount(strWhere);
        }

        /// <summary>
        /// 根据店铺名称关键字获得数据列表
        /// </summary>
        /// <remarks>2016-08-23 万乐园 创建</remarks>
        public List<Shops> GetListByName(string keyWords, bool? isActive = null, string inIds = null)
        {
            var ds = ShopsDAL.Instance.GetListByName(keyWords, isActive, inIds);
            return ServicesHelper.DataSetToIList<Shops>(ds).ToList();
        }

        /// <summary>
        /// 根据店铺全称得到一个对象实体
        /// </summary>
        /// <remarks>2016-08-31 万乐园 创建</remarks>
        public Shops GetModelByName(string name)
        {
            var ds = ShopsDAL.Instance.GetModelByName(name);
            return ds == null ? null : ServicesHelper.DataSetToIList<Shops>(ds).ToList()[0];
        }

        /// <summary>
        /// 根据店铺电话得到一个对象实体
        /// </summary>
        /// <remarks>2016-09-01 万乐园 创建</remarks>
        public Shops GetModelByTel(string tel)
        {
            var ds = ShopsDAL.Instance.GetModelByTel(tel);
            return ds == null ? null : ServicesHelper.DataSetToIList<Shops>(ds).ToList()[0];
        }

        public List<Shops> GetApplyShopList(int cloudCardId)
        {
            return ShopsDAL.Instance.GetApplyShopList(cloudCardId);
        }

        /// <summary>
        /// 根据店铺名称关键字获得分店数据列表
        /// </summary>
        public List<Shops> GetBranchShopList(string keyWords, int parentShopId)
        {
            var ds = ShopsDAL.Instance.GetBranchShopList(keyWords, parentShopId);
            return ServicesHelper.DataSetToIList<Shops>(ds).ToList();
        }

        public Shops GetModelByIdWhereIndustryId(int Id)
        {
            return ShopsDAL.Instance.GetModelByIdWhereIndustryId(Id);
        }
        #endregion
    }
}